{extend name="common/base" /}
{block name="content"}
<div class="row">
	<div class="col-md-12">
		<div class="card">
			<div class="card-header">
				<header class="card-title">
					{:__('speedCache')}
				</header>
			</div>
			<div class="card-body" id="load_status">
				<table class="table table-bordered">
					<thead>
						<tr>
							<th colspan="3">{:__('Status')}：
								{if $status }
									<span style="color:green" >{:__('Open')}<a href="javascript:speed_cache_status('close');">[{:__('Close')}]</a></span>
									{else /}
									<span style="color:#2288cc" >{:__('Close')}<a href="javascript:speed_cache_status('open');">[{:__('Open')}]</a></span>
								{/if}
							</th>
						</tr>
					</thead>
					<tbody>
						<tr>
							<td style="text-align: center;">{:__('Today Hit')}</td>
							<td style="text-align: center;">{:__('Total Hit')}</td>
							<td style="text-align: center;">{:__('Acceleration strategy')}</td>
						</tr>
						<tr>
							<td style="text-align: center;">{$today_hit|default="0"} %</td>
							<td style="text-align: center;">{$total_hit|default="0"} %</td>
							<td style="text-align: center;" onclick="cache_list()"><span class="btn btn-sm btn-info btn-block">{$rule|default="Default"}</span></td>
						</tr>
					</tbody>
				</table>
				<hr>
				<small class="text-muted">
					<span style="color:red"></span> {:__('If your site does not allow anonymous access, please do not enable site acceleration')}<br>
					<span style="color:red"></span> {:__('If your site is an interactive site, the hit rate may be low')}<br>
					<span style="color:red">！</span> {:__('After enabling site acceleration, if site acceleration is found, please turn off site acceleration')}<br>
					<span style="color:red">！</span> {:__('After enabling site acceleration, the update frequency of your website page content depends on the [Cache Period] time')}<br>
				</small>
			</div>
		</div>
	</div>
</div>
<div class="row">
	<div class="col-md-6 col-lg-6" id="loading_box1">
		<div class="card card-hover-shadow">
			<header class="card-header">
				<div class="card-title">{:__('Cache rule')} <!--<a href="javascript:inc('force')" class="btn btn-xs btn-warning">{:__("Add")}</a>-->
				</div>
			</header>
			<div class="card-body">
				<table class="table table-striped table-borderless">
					<tbody>
						{volist name="force.host" id="host"}
						<tr>
							<td>{:__('Domain')}</td>
							<td>{$host}</td>
							<td><a href="javascript:delRule('{$host}','force','host')">{:__('Delete')}</a></td>
						</tr>
						{/volist}
						{volist name="force.ip" id="ip"}
						<tr>
							<td>IP</td>
							<td>{$ip}</td>
							<td><a href="javascript:delRule('{$ip}','force','ip')">{:__('Delete')}</a></td>
						</tr>
						{/volist}
						{volist name="force.args" id="args"}
						<tr>
							<td>{:__('Request parameter')}</td>
							<td>{$args}</td>
							<td><a href="javascript:delRule('{$args}','force','args')">{:__('Delete')}</a></td>
						</tr>
						{/volist}
						{volist name="force.ext" id="ext"}
						<tr>
							<td>{:__('Suffix')}</td>
							<td>{$ext}</td>
							<td><a href="javascript:delRule('{$ext}','force','ext')">{:__('Delete')}</a></td>
						</tr>
						{/volist}
						{volist name="force.type" id="type"}
						<tr>
							<td>{:__('Response type')}</td>
							<td>{$type}</td>
							<td><a href="javascript:delRule('{$type}','force','type')">{:__('Delete')}</a></td>
						</tr>
						{/volist}
						{volist name="force.uri" id="uri"}
						<tr>
							<td>{:__('Url address')}</td>
							<td>{$uri}</td>
							<td><a href="javascript:delRule('{$uri}','force','uri')">{:__('Delete')}</a></td>
						</tr>
						{/volist}
					</tbody>
				</table>
			</div>
		</div>
	</div>
	<div class="col-md-6 col-lg-6" id="loading_box2">
		<div class="card card-hover-shadow">
			<header class="card-header">
				<div class="card-title">{:__('Not cache rule')} <!--<a href="javascript:inc('white')" class="btn btn-xs btn-warning">{:__("Add")}</a>-->
				</div>
			</header>
			<div class="card-body">
				<table class="table table-striped table-borderless">
					<tbody>
						{volist name="white.host" id="host"}
						<tr>
							<td>{:__('Domain')}</td>
							<td>{$host}</td>
							<td><a href="javascript:delRule('{$host}','white','host')">{:__('Delete')}</a></td>
						</tr>
						{/volist}
						{volist name="white.ip" id="ip"}
						<tr>
							<td>IP</td>
							<td>{$ip}</td>
							<td><a href="javascript:delRule('{$ip}','white','ip')">{:__('Delete')}</a></td>
						</tr>
						{/volist}
						{volist name="white.args" id="args"}
						<tr>
							<td>{:__('Request parameter')}</td>
							<td>{$args}</td>
							<td><a href="javascript:delRule('{$args}','white','args')">{:__('Delete')}</a></td>
						</tr>
						{/volist}
						{volist name="white.ext" id="ext"}
						<tr>
							<td>{:__('Suffix')}</td>
							<td>{$ext}</td>
							<td><a href="javascript:delRule('{$ext}','white','ext')">{:__('Delete')}</a></td>
						</tr>
						{/volist}
						{volist name="white.type" id="type"}
						<tr>
							<td>{:__('Response type')}</td>
							<td>{$type}</td>
							<td><a href="javascript:delRule('{$type}','white','type')">{:__('Delete')}</a></td>
						</tr>
						{/volist}
						{volist name="white.uri" id="uri"}
						<tr>
							<td>{:__('Url address')}</td>
							<td>{$uri}</td>
							<td><a href="javascript:delRule('{$uri}','white','uri')">{:__('Delete')}</a></td>
						</tr>
						{/volist}
						{volist name="white.method" id="method"}
						<tr>
							<td>{:__('Request type')}</td>
							<td>{$method}</td>
							<td><a href="javascript:delRule('{$method}','white','method')">{:__('Delete')}</a></td>
						</tr>
						{/volist}
						{volist name="white.cookie" id="cookie"}
						<tr>
							<td>{:__('Cookie')}</td>
							<td>{$cookie}</td>
							<td><a href="javascript:delRule('{$cookie}','white','cookie')">{:__('Delete')}</a></td>
						</tr>
						{/volist}
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>
{/block}
{block name="script"}
<script>
    function speed_cache_status(type){
		var l = $('#load_status').lyearloading({
			opacity: 0.125,
			spinnerSize: 'lg'
		});
		$.post('', {
			status: type
		}, function (res) {
			EchoMsg(res.msg, 1);
		})
		.fail(function () {
			EchoMsg('{:__("Request error, please try again later")}');
		})
		.always(function () {
			l.destroy();
		})
    }

	function delRule(rule_content,type1, type) {
		id_text = type1 == 'white' ? 'loading_box2' : 'loading_box1';
		$.confirm({
			title: '{:__("Reminder")}',
			content: '{:__("Confirm delete?")}',
			icon: 'mdi mdi-alert',
			animation: 'scale',
			closeAnimation: 'zoom',
			buttons: {
				confirm: {
					text: '{:__("Confirm")}',
					btnClass: 'btn-orange',
					action: function () {
						var l = $('#'+id_text).lyearloading({
							opacity: 0.125,
							spinnerSize: 'lg'
						});
						$.post('{:url("index/vhost/speed_cache_del")}', {
								rule_content:rule_content,
								rule_root: type1,
								rule_type: type
							}, function (res) {
								EchoMsg(res.msg, 1);
							})
							.fail(function () {
								EchoMsg('{:__("Request error, please try again later")}');
							})
							.always(function () {
								l.destroy();
							})
					}
				},
				'{:__("Cancel")}': function () {}
			}
		});
	}

	function inc(){
		// TODO 添加缓存/不缓存规则
	}

	function cache_list(){
		var l = $('#load_status').lyearloading({
			opacity: 0.125,
			spinnerSize: 'lg'
		});
		$.post('{:url("index/vhost/speed_cache_list")}', '', function (res) {
			if(res){
				var list_content = '';
				$.each(res,function(i,v){
					list_content+='<tr>';
					list_content+= '<td>'+v.name+'</td>';
					list_content+= '<td>'+v.ps+'</td>';
					list_content+= '<td><a class="btn btn-sm btn-success" onclick="set_rule(\''+v.name+'\')">{:__("Application")}</a></td>';
					list_content+='</tr>';
				});
				content = '<div class="row"><div class="col-lg-12"><table class="table"><tbody class="rule_list">'+list_content+'</tbody></table></div></div>';
				$.alert({
					title: '内置规则列表',
					boxWidth: [$(window).width() > 800 ? '800px' : '60%', $(window).height() > 600 ? '600px' : '95%'],
					content: content,
					animation: 'scale',
					closeAnimation: 'bottom',
					backgroundDismiss: true,
					draggable: true,
					icon: 'mdi mdi-rocket',
				});
			}
		})
		.fail(function () {
			EchoMsg('{:__("Request error, please try again later")}');
		})
		.always(function () {
			l.destroy();
		})
	}

	function set_rule(name){
		var l = $('.jconfirm-box').lyearloading({
			opacity: 0.125,
			spinnerSize: 'lg'
		});
		$.post('', {
			ruleName:name,
		}, function (res) {
			EchoMsg(res.msg, res.code);
		})
		.fail(function () {
			EchoMsg('{:__("Request error, please try again later")}');
		})
		.always(function () {
			l.destroy();
		})
	}
</script>
{/block}